﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;

namespace GenericLibrary.ExtensionMethods {

    public static class XmlWriterExtension {

        public delegate void XmlObjectWriter1<T>(T t);
        public delegate void XmlObjectWriter2<T>(T t, XmlWriter writer);

        public static void Write<T>(this XmlWriter xmlWriter, XmlObjectWriter1<T> objWriter, IEnumerable<T> objects) {
            try {
                foreach(T t in objects) {
                    objWriter(t);
                }
            } catch(Exception) {
                Debug.Assert(false);
                throw;
            }
        }

        public static void Write<T>(this XmlWriter xmlWriter, XmlObjectWriter1<T> objWriter, IEnumerable objects) {
            try {
                foreach(T t in objects) {
                    objWriter(t);
                }
            } catch(Exception) {
                Debug.Assert(false);
                throw;
            }
        }

        public static void Write<T>(this XmlWriter xmlWriter, XmlObjectWriter1<T> objWriter, params T[] objects) {
            try {
                foreach(T t in objects) {
                    objWriter(t);
                }
            } catch(Exception) {
                Debug.Assert(false);
                throw;
            }
        }

        public static void Write<T>(this XmlWriter xmlWriter, XmlObjectWriter2<T> objWriter, IEnumerable<T> objects) {
            try {
                foreach(T t in objects) {
                    objWriter(t, xmlWriter);
                }
            } catch(Exception) {
                Debug.Assert(false);
                throw;
            }
        }

        public static void Write<T>(this XmlWriter xmlWriter, XmlObjectWriter2<T> objWriter, params T[] objects) {
            try {
                foreach(T t in objects) {
                    objWriter(t, xmlWriter);
                }
            } catch(Exception) {
                Debug.Assert(false);
                throw;
            }
        }

        public static void Write<T>(this XmlWriter xmlWriter, XmlObjectWriter2<T> objWriter, IEnumerable objects) {
            try {
                foreach(T t in objects) {
                    objWriter(t, xmlWriter);
                }
            } catch(Exception) {
                Debug.Assert(false);
                throw;
            }
        }
    }
}
